-- to_layer
-- Created by chengb Jul/10/2015
-- 成就：达成迷宫层数

-- 内部函数声明
local whenNextFloor;
local checkCondition;

-- 关注玩家进入下一层的回调
EventMgr.register("achievements/to_layer", event.NEXT_FLOOR, function()
    whenNextFloor();
end);

-- 关注玩家进入下一层的回调
whenNextFloor = function()
    -- 获取当前所在的迷宫编号
    local dungeonId = DungeonM.getDungeonId();

    local layer = DungeonM.currentLayer();

    -- 遍历所有的子规则
    local ids = AchievementM.getIdsByRule("to_layer");

    -- 判断是否是符合要求的成就
    local function isValidId(id)
        if true == AchievementM.isCompleted(id) then
            -- 已经完成了
            return false;
        end

        if not AchievementM.isBelongTo(id, dungeonId) then
        	-- 不是该迷宫的成就
        	return false;
        end

        local config = AchievementM.query(id);
        local ruleArg = config["rule_arg"];
        if type(ruleArg) == 'table' then
            -- 判断是否是需求层
            if type(ruleArg["layer"]) == 'number' and
                ruleArg["layer"] ~= layer then
                return false;
            end

            -- 如果配置了条件，还有额外检查
            if type(ruleArg["condition"] == 'string') then
                if not checkCondition(ruleArg["condition"]) then
                    return false;
                end
            end
        end

        return true;
    end

    for _, id in pairs(ids) do
        if isValidId(id) then
            -- 尝试完成成就
        	AchievementM.tryComplete(id, 1);
        end
    end
end

-- 检查条件
function checkCondition(condition)
    if "without_bine" == condition then
        -- 不能种植魔藤

        if ME.user.bine ~= nil and ME.user.bine.classId == PLANT_CLASS_BINE then
    	    return false;
    	end
    elseif "unclose_stone" == condition then
        -- 不能解除结界

        if 1 == DungeonM.getDungeonMixedField("has_closed_stone") then
            return false;
        end
    elseif "without_ship" == condition then
        -- 不能雇佣幽灵船

        if ME.user.ship ~= nil then
            return false;
        end
    elseif "without_lamp_wish" == condition then
        -- 不使用神灯许愿

        if BuffDataM.query("lamp_already_used", 1) ~= 0 then
            return false;
        end
    elseif "without_demon_statue" == condition then
        -- 不献祭

        local demonExp = ME.user.dbase:query("demon_exp");
        if demonExp then
            return false;
        end
    elseif "without_holy_items" == condition then
        -- 不能使用圣器

        if HolyItemsM.hasHolyItems() then
            return false;
        end
    elseif "without_warning" == condition then
        -- 不能让预警III型呼唤同伴

        if 1 == DungeonM.getDungeonMixedField("monster_warning") then
            return false;
        end
    elseif "without_learn_manual" == condition then
        -- 不能学习工程学手册

        if 1 == DungeonM.getDungeonMixedField("has_learn_manual") then
            return false;
        end
    end

    return true;
end

return {
    -- 获取成就描述
    queryDesc = function(id)
        local config = AchievementM.query(id);

        local ruleArg = config["rule_arg"];
        local layer   = ruleArg["layer"];
        local desc    = config["desc"];

        desc = string.gsub(desc, "{0}", layer);

        return desc;
    end,
};